﻿package export.garden
{
	import com.dusk.util.TextUtil;
	import com.game.manager.GameSceneManager;
	import com.game.manager.NotificationManager;
	import com.game.view.component.notification.TipBox;
	
	import config.Config;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class GardenInterface extends Sprite
	{
		
		
		public function GardenInterface()
		{
			super();
			TextUtil.redrawTxtOld(this, ["exptxt", "titletxt"]);
			this.addEventListener(Event.ADDED_TO_STAGE, added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removed, false, 0, true);
		}
		
		public var land1:MovieClip;
		public var land2:MovieClip;
		public var land3:MovieClip;
		public var land4:MovieClip;
		public var land5:MovieClip;
		public var land6:MovieClip;
		public var land7:MovieClip;
		public var land8:MovieClip;
		public var land9:MovieClip;
		public var btn_close:SimpleButton;
		public var btn_worship:SimpleButton;
		public var expmc:MovieClip;
		public var exptxt:TextField;
		public var titletxt:TextField;
		public var templeMC:MovieClip;
		
		private function initLandGodShow():void
		{
			this.btn_worship.visible = false;
			if (Config.getIns().userData.gardenLevel > 3)
			{
				this.btn_worship.visible = true;
			}
		}
		
		private function setTxt():void
		{
			var lv:uint = Config.getIns().userData.gardenLevel;
			this.titletxt.text = "药园Lv." + lv;
			if (lv >= 9)
			{
				this.titletxt.appendText("(Max)");
			}
			this.exptxt.text = Config.getIns().userData.gardenExp + "/" + String(1000);
			this.expmc.gotoAndStop(int(Config.getIns().userData.gardenExp / 1000 * 100));
		}
		
		private function initLand():void
		{
			var i:* = 1;
			var lv:int = Config.getIns().userData.gardenLevel;
			for (i; i <= lv; i++)
			{
				this["land" + i].gotoAndStop("planted");
			}
			if (lv <= 3)
			{
				this["land" + lv].gotoAndStop("lowCase");
			}
			else if (lv >= 6)
			{
				this["land" + lv].gotoAndStop("highCase");
			}
			else
			{
				this["land" + lv].gotoAndStop("midCase");
			}
			for (i; i <= 9; i++)
			{
				this["land" + i].gotoAndStop("undeveloped");
			}
			this.templeMC.gotoAndStop(Math.ceil(lv / 2));
		}
		
		private function added(e:Event):void
		{
			var i:* = 0;
			for (i = 1; i <= 9; i++)
			{
				this["land" + i].buttonMode = true;
				this["land" + i].addEventListener(MouseEvent.CLICK, landClick);
			}
			this.setTxt();
			this.initLand();
			this.initLandGodShow();
			this.templeMC.buttonMode = true;
			this.templeMC.addEventListener(MouseEvent.CLICK, showTempleInfo);
			this.btn_close.addEventListener(MouseEvent.CLICK, xClick);
			this.btn_worship.addEventListener(MouseEvent.CLICK, intoWorship);
		}
		
		private function removed(e:Event):void
		{
			var i:* = 0;
			for (i = 1; i <= 9; i++)
			{
				this["land" + i].removeEventListener(MouseEvent.CLICK, landClick);
			}
			this.templeMC.removeEventListener(MouseEvent.CLICK, showTempleInfo);
			this.btn_close.removeEventListener(MouseEvent.CLICK, xClick);
			this.btn_worship.removeEventListener(MouseEvent.CLICK, intoWorship);
		}
		
		private function showTempleInfo(param1:MouseEvent):void
		{
			var _loc1_:TipBox = new TipBox("药园等级:Lv." + Config.getIns().userData.gardenLevel + "\n", null, null, false);
			_loc1_.appendText("土地庙等级:Lv." + Math.ceil(Config.getIns().userData.gardenLevel / 2) + "\n");
			_loc1_.add();
		}
		
		private function landClick(param1:MouseEvent):void
		{
			var lid:uint = int(String(param1.currentTarget.name).substr(4, 1));
			var type:String = this["land" + lid].currentLabel;
			switch (type)
			{
				case "planted":
					NotificationManager.alert("当前等级无法种植该土地");
					break;
				case "undeveloped":
					NotificationManager.alert("土地尚未开拓");
					break;
				default:
					var _loc1_:HarvestPlant = new HarvestPlant(Config.getIns().userData.gardenLevel, type);
					GameSceneManager.getIns().addScene(_loc1_);
			}
		}
		
		private function intoWorship(param1:MouseEvent):void
		{
			var _loc1_:WorshipFlower = new WorshipFlower();
			GameSceneManager.getIns().addScene(_loc1_);
		}
		
		private function xClick(e:MouseEvent):void
		{
			if (this.parent)
			{
				this.parent.removeChild(this);
			}
			GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MAP);
		}
	}
}
